package com.ruoyi.web.controller.warehouse;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MaterialInventory;
import com.ruoyi.system.response.MaterialInventoryList;
import com.ruoyi.system.service.IMaterialInventoryService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料库存Controller
 *
 * @author tanshen
 * @date 2023-08-21
 */
@Controller
@RequestMapping("/warehouse/materialInventory")
public class MaterialInventoryController extends BaseController {
    private String prefix = "warehouse/materialInventory";

    @Autowired
    private IMaterialInventoryService materialInventoryService;

    @RequiresPermissions("warehouse:materialInventory:view")
    @GetMapping()
    public String materialInventory() {
        return prefix + "/materialInventory";
    }

    /**
     * 查询物料库存列表
     */
    @RequiresPermissions("warehouse:materialInventory:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(MaterialInventory materialInventory) {
        startPage();
        List<MaterialInventoryList> list = materialInventoryService.selectMaterialInventoryList(materialInventory);
        return getDataTable(list);
    }

    /**
     * 导出物料库存列表
     */
    @RequiresPermissions("warehouse:materialInventory:export")
    @Log(title = "物料库存", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MaterialInventory materialInventory) {
        List<MaterialInventoryList> list = materialInventoryService.selectMaterialInventoryList(materialInventory);
        ExcelUtil<MaterialInventoryList> util = new ExcelUtil<>(MaterialInventoryList.class);
        return util.exportExcel(list, "物料库存数据");
    }

    /**
     * 新增物料库存
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存物料库存
     */
    @RequiresPermissions("warehouse:materialInventory:add")
    @Log(title = "物料库存", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MaterialInventory materialInventory) {
        return toAjax(materialInventoryService.insertMaterialInventory(materialInventory));
    }

    /**
     * 修改物料库存
     */
    @RequiresPermissions("warehouse:materialInventory:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        MaterialInventory materialInventory = materialInventoryService.selectMaterialInventoryById(id);
        mmap.put("materialInventory", materialInventory);
        return prefix + "/edit";
    }

    /**
     * 修改保存物料库存
     */
    @RequiresPermissions("warehouse:materialInventory:edit")
    @Log(title = "物料库存", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MaterialInventory materialInventory) {
        return toAjax(materialInventoryService.updateMaterialInventory(materialInventory));
    }

    /**
     * 删除物料库存
     */
    @RequiresPermissions("warehouse:materialInventory:remove")
    @Log(title = "物料库存", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(materialInventoryService.deleteMaterialInventoryByIds(ids));
    }
}
